/*
 * Copyright [2008] by Edward Chang
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package per.edward.wizard.ext;

import java.util.ArrayList;
import java.util.List;

import com.nexes.wizard.Wizard;

/**
 * A proxy to create a Wizard object conveniently.
 * <p>
 * It provides following convenient methods:
 * <li>accepts a couple of <code>CustomDescriptoer</code> (which is a sub-class
 * of <code>WizardPanelDescriptor</code> class) instances to create a Wizard
 * object.</li>
 * <li>gathers <code>WizardDataMap</code> of each CustomDescriptor it contains
 * and return a merged WizardDataMap<String, Entry<String, String> []>.</li>
 * </p>
 * 
 * @author xzhang
 * 
 */
public class WizardProxy {

    private final List<CustomDescriptor> descriptors_ = new ArrayList<CustomDescriptor>();

    private final Wizard wizard_ = new Wizard();

    /**
     * Default constructor.
     */
    public WizardProxy() {
    }

    /**
     * A convenient constructor accepts a couple of CustomDescriptors.
     * 
     * @param descriptors
     */
    public WizardProxy(CustomDescriptor[] descriptors) {
        addDescriptors(descriptors);
    }

    /**
     * A convenient method for registering a CustomDescriptor.
     * 
     * @param wizard
     * @param descriptor
     */
    public void addDescriptor(CustomDescriptor descriptor) {

        if (wizard_ == null) {
            throw new IllegalArgumentException(
                    "Requires a non-null Wizard object instance.");
        }

        wizard_.registerWizardPanel(descriptor.getPanelDescriptorIdentifier(),
                descriptor);
        descriptors_.add(descriptor);
        
        // set first added descriptor as current panel descriptor.
        if(descriptors_.size() == 1) {
            wizard_.setCurrentPanel(descriptor.getPanelDescriptorIdentifier());
        }
    }

    /**
     * A convenient method for registering a couple of Wizard Panel Descriptors.
     * 
     * @param wizard
     * @param descriptors
     */
    public void addDescriptors(CustomDescriptor[] descriptors) {

        if (descriptors == null) {
            throw new IllegalArgumentException(
                    "Requires a non-null WizardPanelDescriptor classes array.");
        }
        for (int i = 0, n = descriptors.length; i < n; i++) {
            addDescriptor(descriptors[i]);
        }
    }

    public WizardDataMap getMergedWizardDataMap() {
        WizardDataMap map = new WizardDataMap();
        for (CustomDescriptor desc : descriptors_) {
            map.put(desc.getPanelDescriptorIdentifier(), desc
                    .getWizardDataMap());
        }

        return map;
    }

    /**
     * Provide this interface hence to fetch Wizard Object and make
     * corresponding operations.
     * 
     * @return
     */
    public Wizard getWizard() {
        return wizard_;
    }
}
